#ifndef __DICTPRODUCER_H__
#define __DICTPRODUCER_H__

#include <vector>
#include <map>
#include <set>

using std::vector;
using std::pair;
using std::string;
using std::map;
using std::set;

class SplitTool;
class Configuration;

class DictProducer
{
public:
    DictProducer(const string &dir);
    DictProducer(const string &dir, SplitTool *splitTool);
    ~DictProducer();

    void buildEnDict();
    void buildCnDict(Configuration *pconf);
    void createIndex();
    void storeDict(const char *filepath);

    /* void showFiles() const; */
    void showDict() const;

    void pushFiles(const string &);
    string getFiles();
    void pushDict(const string &word);

private:
    vector<string> _files;
    //vector<pair<string, int>> _dict;
    map<string, int> _dict;
    map<string, set<int>> _index;
    SplitTool *_splitTool;
};

inline
size_t nBytesCode(const char ch) {
	if(ch & (1 << 7)) {
		int nBytes = 1;
		for(int idx = 0; idx != 6; ++idx) {
			if(ch & (1 << (6 - idx))) {
				++nBytes;	
			}
			else
				break;
		}
		return nBytes;
	}
	return 1;
}  

inline
std::size_t getLen(const std::string &str)
{
	std::size_t ilen = 0;
	for(std::size_t idx = 0; idx != str.size(); ++idx)
	{
		int nBytes = nBytesCode(str[idx]);
		idx += (nBytes - 1);
		++ilen;
	}
	return ilen;
}

#endif
